package com.example.user_module;


import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.viewbinding.ViewBinding;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.LogUtils;
import com.example.base_application.BaseApplication;
import com.example.common.EvenBusAction;
import com.example.common.mvvm.view.BaseFragment;
import com.example.loadimg_sdk.ImgLoad;
import com.example.user_module.databinding.FragmentUserBinding;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Map;

public class UserFragment  extends BaseFragment<FragmentUserBinding,UserViewModel> implements IUiListener , View.OnClickListener {
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refreshUser(EvenBusAction evenBusAction){
        if (evenBusAction.action.equals("refresh")){
            vm.refreshUser();
        }
    }
    @Override
    public View bindLayout() {
        return v.getRoot();
    }

    @Override
    public ViewBinding initBinding() {
        FragmentUserBinding binding=FragmentUserBinding.inflate(inflater);
        return binding;
    }

    @Override
    public void initView() {
        v.avatar.setOnClickListener(this);
        vm.liveDataBus.observe(this, new Observer<Map<String, Object>>() {
            @Override
            public void onChanged(Map<String, Object> map) {
                String nickName = (String) map.get("nickName");
                String figureurl = (String) map.get("figureurl");
                v.name.setText(nickName);
                ImgLoad.loadCircleImg(figureurl,v.avatar);
            }
        });
    }

    @Override
    public void initData() {

    }

    @Override
    public void onComplete(Object o) {

    }

    @Override
    public void onError(UiError uiError) {

    }

    @Override
    public void onCancel() {

    }

    @Override
    public void onClick(View view) {
        if (view==v.avatar){
            vm.login();
        }
    }
}
